package com.briup.ch08;

import java.util.Comparator;
import java.util.TreeMap;

public class Test9_TreeMap {
    public static void main(String[] args) {
        //key-Integer :自然排序，implements Comparable-comparaTo
        TreeMap<Integer,String> tree1=new TreeMap<>();
        tree1.put(100,"hello");
        tree1.put(50,"world");
        tree1.put(200,"book");
        tree1.put(150,"computer");
        tree1.putIfAbsent(100,"hehe");
        //按照k的从小到大排序
        System.out.println(tree1);
        //comparator比较器：奇数前，偶数在后，奇数正序，偶数倒叙
        TreeMap<Integer,String> tree2=new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                boolean odd1=o1%2==0;//true 偶数 false 奇数
                boolean odd2=o2%2==0;//true 偶数 false 奇数
                //都是偶数
                if(odd1&&odd2){
                    return o2-o1;
                }else if(!odd1&&!odd2){
                    //都是奇数
                    return o1-o2;
                }else if(odd1&&!odd2){
                    //odd2一奇数odd1一偶数
                    //odd2待排序：左边
                    return 1;
                }else if(!odd1&&odd2){
                    //odd1一奇数odd2一偶数
                    //odd2待排序：右边
                    return -1;
                }
                return 0;
            }
        });
        tree2.put(100,"hello");
        tree2.put(99,"hello");
        tree2.put(200,"hello");
        tree2.put(87,"hello");
        tree2.put(400,"hello");
        tree2.put(55,"hello");
        System.out.println(tree2);

    }
}
